package com.bbh.plugin.single.privilege.core.service;

import com.bbh.common.base.service.BaseService;
import com.bbh.plugin.single.privilege.core.entity.AccountRole;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;
@Service
@Transactional
public class AccountRoleService extends BaseService<AccountRole,String>{
    public int updateRole(String[] ids,String[] roleIds, String accountId){
        int count=0;
        //根据userid roleids 查询并删除
        AccountRole accountRole;
         if(StringUtils.isNotEmpty(accountId)&&accountId.length()>0){
             if(ArrayUtils.isNotEmpty(roleIds)){
                 for(String roleId:roleIds){
                     accountRole=new AccountRole();
                     accountRole.setAccountId(accountId);
                     accountRole.setRoleId(roleId);
                     List<AccountRole> roleList=super.select(accountRole);

                     if(roleList.size()>0){
                         for(AccountRole role:roleList){
                             super.deleteByPrimaryKey(role.getId());
                         }
                     }
                 }
             }
             if(ArrayUtils.isNotEmpty(ids)){
                 //增加新的role
                 for(String id:ids){
                     accountRole=new AccountRole();
                     accountRole.setRoleId(id);
                     accountRole.setAccountId(accountId);
                     accountRole.setId(UUID.randomUUID().toString().replace("-", ""));
                     super.insert(accountRole);
                 }
             }
             count=1;
         }
        return count;
    }
}
